package com.zzg.springboot;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.zzg.springboot.filter.MyFilter;
import com.zzg.springboot.listener.MyListener;
import com.zzg.springboot.servlet.MyServlet;

@SpringBootApplication
public class Application {
	/**
	 * 
	 * @Title: regisServlet   
	 * @Description: springboot 注册自定义Servlet,通过  ServletRegistrationBean绑定。
	 * @param: @return      
	 * @return: ServletRegistrationBean      
	 * @throws
	 */
	@Bean
	public ServletRegistrationBean  regisServlet(){
		return new ServletRegistrationBean(new MyServlet(),"/servlet/*");
	}
	
	/**
	 * 
	 * @Title: regisFilter   
	 * @Description: springboot 注册自定义Filter,通过  FilterRegistrationBean绑定。   
	 * @param: @return      
	 * @return: FilterRegistrationBean      
	 * @throws
	 */
	@Bean
	public FilterRegistrationBean  regisFilter(){
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		// 设置Filter 名称
		registrationBean.setName("myFilter");
		MyFilter myFilter = new MyFilter();
		registrationBean.setFilter(myFilter);
		// 设置Filter优先级
		registrationBean.setOrder(1);
		// 设置Filter 拦截路径
		registrationBean.setUrlPatterns(Arrays.asList("/*"));
		return registrationBean;
	}
	
	/**
	 * 
	 * @Title: regisFilter   
	 * @Description: springboot 注册自定义Listener,通过  ServletListenerRegistrationBean<T>绑定。   
	 * @param: @return      
	 * @return: FilterRegistrationBean      
	 * @throws
	 */
	@Bean
	public ServletListenerRegistrationBean<MyListener>  regisListener(){
		ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<MyListener>();
		registrationBean.setListener(new MyListener());
		return registrationBean;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(Application.class, args);
	}

}
